﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

using Excel = Microsoft.Office.Interop.Excel;

namespace Common
{
    class ExcelApp
    {

        private Excel._Application oExcel = null;
        private Excel.Workbook workbook = null;
        private Excel.Worksheet sheet = null;

        private object missingValue = System.Reflection.Missing.Value;

        public void open(string path) {
            string fileName = new FileInfo(path).FullName;

            if (oExcel == null)
            {
                oExcel = new Excel.ApplicationClass();
            }
            //UserControl(false) ;

            workbook = oExcel.Workbooks.Open(
                fileName, missingValue, missingValue,
                 missingValue, "abc123", missingValue,
                 missingValue, missingValue, missingValue,
                 missingValue, missingValue, missingValue,
                 missingValue, missingValue, missingValue);

            //Excel.Workbook workbook = oExcel.ActiveWorkbook;
            sheet = (Excel.Worksheet)workbook.ActiveSheet;
        }

        public void printAndClose(string path)
        {


            sheet.PrintOut(missingValue, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);


            if (!string.IsNullOrEmpty(path))
            {
                string fileName = new FileInfo(path).FullName;
                workbook.SaveAs(fileName, missingValue, missingValue, missingValue, missingValue,
                         missingValue, Excel.XlSaveAsAccessMode.xlNoChange,
                         Excel.XlSaveConflictResolution.xlLocalSessionChanges,
                         missingValue, missingValue, missingValue, missingValue);
            }

            workbook.Close(false, missingValue, missingValue);
        }

        public void saveAndClose(string path)
        {
            if (!string.IsNullOrEmpty(path))
            {
                string fileName = new FileInfo(path).FullName;
                workbook.SaveAs(fileName, missingValue, missingValue, missingValue, missingValue,
                         missingValue, Excel.XlSaveAsAccessMode.xlNoChange,
                         Excel.XlSaveConflictResolution.xlLocalSessionChanges,
                         missingValue, missingValue, missingValue, missingValue);
            }

            workbook.Close(false, missingValue, missingValue);
        }

        public void quitNotSave() {
            if (oExcel != null)
            {
                oExcel.Quit();
            }
            oExcel = null;
            workbook = null;
            sheet = null;
        }

        public void excelOut(String contain, int rowIndex, int cellIndex) {

            Excel.Range cellRange = sheet.Cells[rowIndex, cellIndex] as Excel.Range;

            if (cellRange.NumberFormat == null || cellRange.NumberFormat.ToString() == "G/通用格式" || cellRange.NumberFormat.ToString() == "@")
            {
                sheet.Cells[rowIndex, cellIndex] = contain;
            }
            else
            {
                try
                {
                    sheet.Cells[rowIndex, cellIndex] = double.Parse(contain);
                }
                catch
                {
                    sheet.Cells[rowIndex, cellIndex] = contain;
                }
            }
        }

        public void excelOut(String[] contains, int offset, bool isCopyRow)
        {
            try
            {
                int excelRowIndex = 6;
                int rowIndex = excelRowIndex + offset;

                if (isCopyRow)
                {
                    // insert one row
                    Excel.Range range = sheet.Cells[rowIndex + 1, 1] as Excel.Range;

                    range.Select();

                    range.EntireRow.Insert(Type.Missing, Type.Missing);
                }



                if (contains != null)
                {
                    //sheet.Cells[rowIndex, 1] = "" + (offset + 1);
                    for (int i = 0; i < contains.Length; i++)
                    {
                        //Console.WriteLine("value:" + i + "-" + contains[i]);
                        /*
                        Excel.Range cellRange = sheet.Cells[rowIndex, i + 2] as Excel.Range;
                        if (cellRange.NumberFormat == null || cellRange.NumberFormat.ToString() == "G/通用格式" || cellRange.NumberFormat.ToString() == "@")
                        {
                            sheet.Cells[rowIndex, i + 2] = contains[i];
                        }
                        else
                        {
                            try
                            {
                                sheet.Cells[rowIndex, i + 2] = double.Parse(contains[i]);
                            }
                            catch
                            {
                                sheet.Cells[rowIndex, i + 2] = contains[i];
                            }
                        }*/
                        excelOut(contains[i], rowIndex, i + 2);
                        //sheet.Cells[rowIndex, i + 2] = 2.0;
                    }
                }

            }
            catch
            {
                throw;
            }


        }



    }
}
